package dw2;

import java.awt.*;
import java.awt.Color;
import java.awt.Image;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Arrays;


/**
 *
 * @author Adam, based primarily on npcRider
 */
public class npcSandrat extends NPC {
    protected Particle particles[];
    protected int blinkSpeed = 8;
    protected double angle;
    protected double speed;
    protected double CHASE_SPEED = 2;
    protected int health = 1;
    protected int frames = 2;
    protected int cycle_count = 0;

    protected static ArrayList<Image> image = new ArrayList<Image>();
    public static void initImage(Image images[])
    {
        image.addAll(Arrays.asList(images));
    }
    public npcSandrat(int x, int y, int direction_to_face,int points)
    {

        super(x,y-image.get(0).getHeight(null),-direction_to_face,points);
        //graphics.add(new DWGraphics(image.get(0)));
        graphics.add(new DWGraphics_Animation(image.get(0),50,3));
        graphics.add(new DWGraphics_Animation(image.get(1),50,3));
        graphics.add(new DWGraphics_Animation(image.get(2),blinkSpeed,4));
        graphics.add(new DWGraphics_Animation(image.get(2),blinkSpeed/2,4));
        particles = new Particle[100];
    }
  
    public void move(Main mainparent,Point head_pos)
    {
        //From Bullet class: had to reuse a small ammount of code to insert CHASE_SPEED into the calculation
        double xDiff = head_pos.x - this.x;
        double yDiff = head_pos.y - this.y;
        double H = Math.sqrt((xDiff)*(xDiff) + (yDiff)*(yDiff)); // The hypotenuse
        angle = Math.acos(xDiff/H);
        angle = angle * 57.2960285258;
        if(yDiff < 0)
        {
            angle = 360-angle;
        }
        speed = CHASE_SPEED;
        double radians = Math.toRadians(angle);
       
        x += Worm.getXspeed(angle, speed);
        y += Worm.getYspeed(angle, speed);
        // x += Math.cos(radians) * CHASE_SPEED;
        // y += Math.sin(radians) * CHASE_SPEED;

        if(y < 320) y = 320;

        if(y < head_pos.y) y += 1;
    }

    public void draw(Graphics g,Main mainparent)
    {
        int ymove = 0;
        int move = mainparent.getSize().width/2;
        int shakeamount = mainparent.getShakeAmount();
        move -= mainparent.getScroll();

        if(mainparent.isShaking())
        {
            move += (random.nextInt(shakeamount*2)-shakeamount);
            ymove = (random.nextInt(shakeamount*2)-shakeamount);
        }
        graphics.get(graphics_index).draw(g,x+move,y+ymove,direction,angle);

    }

    public void action(Main mainparent,Worm player)
    {
        cycle_count++;
        this.player = player;
        
        
        if(state == 0){
            
            digOut();
        }
        if(state == 1){
  
            digOut2();
            //cycle_count++;
        }
        if(state == 2 || state == 3){
            //Only move while chasing
            move(mainparent, player.getHeadPosition());
            chaseWorm();
            //cycle_count++;
        }
        if(state == 3){
            //Only move while chasing
            move(mainparent, player.getHeadPosition());
            chaseWorm2();
            //cycle_count++;
        }
        
         if(player.hitWorm(graphics.get(graphics_index).getBounds())== true) {

            // mainparent.bite6SoftNibble.play();

            if((state == 0 || state == 1) && player.hitHead(graphics.get(graphics_index).getBounds())) { //RECEIVE POINTS
                mainparent.bite6SoftNibble.play();
                player.add_points(points);
                player.countKill();
                mainparent.removeNPC(this);
                mainparent.sandratOnScreen = false;
                deathParticles(mainparent,player);
            }
            if(state == 2 || state == 3) { //HURT PLAYER
                player.hurtWorm(10);
                mainparent.removeNPC(this);
                mainparent.sandratOnScreen = false;
                deathParticles(mainparent,player);
                return;
            }


            
        }
   
        if(cycle_count == 150){
            state = 1;
            //cycle_count = 0;
        }
        if(cycle_count == 300){
            state = 2;
            //cycle_count = 0;
        }
        if(cycle_count == 450){
            state = 3;
            CHASE_SPEED *= 1.5;
            //cycle_count = 0;
        }

        if(cycle_count == 550) {
            mainparent.removeNPC(this);
            mainparent.sandratOnScreen = false;
            deathParticles(mainparent,player);
            return;
        }
         
         
    }
    public void digOut(){
        graphics_index = 0;
    }
    public void digOut2(){
        graphics_index = 1;
    }

    public void chaseWorm(){
        graphics_index = 2;
    }
    public void chaseWorm2(){
        graphics_index = 3;
    }

    public void deathParticles(Main mainparent,Worm player)
    {
        int center_x = x + graphics.get(graphics_index).getWidth()/2;
        int center_y = y + graphics.get(graphics_index).getHeight();
        for(int w = 0;w<death_particle_count;w++)
        {
            Particle particle = new Particle(center_x,center_y,Color.RED,2,5);
            particle.toggleRemoveOnGround();
            particle.setLife(15);
            particle.setGravity(0.2);
            mainparent.addParticle(particle);
            particles[w] = particle;
        }

    }
}
